package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.entity.Device;
import com.lin.entity.WarningDevice;
import com.lin.mapper.WarningDeviceMapper;
import com.lin.service.WarningDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lin
 * @since 2022-06-17
 */
@Service
public class WarningDeviceServiceImpl extends ServiceImpl<WarningDeviceMapper, WarningDevice> implements WarningDeviceService {

    @Resource
    WarningDeviceMapper warningDeviceMapper;

    @Override
    public Page<WarningDevice> getWarningDevice(Integer page,Integer size,Integer departmentDid, String key) {
        Page<WarningDevice> page1 = new Page<>(page, size);
        LambdaQueryWrapper<WarningDevice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(departmentDid != 0, WarningDevice::getDepartmentDid, departmentDid).like(StringUtils.isNotBlank(key), WarningDevice::getCode, key);
        return page(page1,queryWrapper);
    }

    @Override
    public Boolean addWarningDevice(WarningDevice warningDevice) {
        //检查字段完整性
        if (warningDevice.getCode() == null || warningDevice.getName() == null || warningDevice.getName().isEmpty() || warningDevice.getCode().isEmpty())
            return false;//这里应该抛出异常，这里先false处理
        return this.save(warningDevice);
    }

    @Override
    public Boolean updateWarningDevice(WarningDevice warningDevice) {
        return this.updateById(warningDevice);
    }

    @Override
    public void removeWarningDevice(Integer id) {
        warningDeviceMapper.deleteById(id);
    }
}
